package com.wakala.employment.adapter;

import static com.xuexiang.xutil.XUtil.getContext;

import android.view.ViewGroup;
import android.widget.TextView;

import com.google.common.collect.Lists;
import com.wakala.employment.R;
import com.wakala.employment.adapter.base.broccoli.BroccoliRecyclerAdapter;
import com.wakala.employment.adapter.entity.JobInfoVo;
import com.wakala.employment.utils.DemoDataProvider;
import com.wakala.employment.utils.PlaceholderHelper;
import com.xuexiang.xui.adapter.recyclerview.RecyclerViewHolder;
import com.xuexiang.xui.utils.DensityUtils;
import com.xuexiang.xutil.common.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import me.samlss.broccoli.Broccoli;

/**
 * @author xuexiang
 * @since 2019/4/7 下午12:06
 */
public class JobInfoListAdapter extends BroccoliRecyclerAdapter<JobInfoVo> {
    /**
     * 是否是加载占位
     */
    private boolean mIsAnim;

    private ItemClickListener<JobInfoVo> itemClickListener;


 /*   public JobInfoListAdapter(boolean isAnim) {
        super(DemoDataProvider.getEmptyJobInfo());
        mIsAnim = isAnim;
    }*/
    public JobInfoListAdapter(boolean isAnim,ItemClickListener<JobInfoVo> itemClickListener) {
        super(DemoDataProvider.getEmptyJobInfo());
        mIsAnim = isAnim;
        this.itemClickListener = itemClickListener;
    }


    @Override
    protected int getItemLayoutId(int viewType) {
        return R.layout.adapter_jobs_card_view_list_item;
    }

    /**
     * 绑定控件
     *
     * @param holder
     * @param model
     * @param position
     */
    @Override
    protected void onBindData(RecyclerViewHolder holder, JobInfoVo model, int position) {
        holder.text(R.id.tv_user_name, model.getUser().getNickname());
        holder.text(R.id.tv_tag, model.getJobTags());
        holder.text(R.id.tv_title, model.getTitle());
        holder.text(R.id.tv_summary, model.getContent());
        holder.text(R.id.tv_praise, model.getPublishTime().substring(0, 16));
        holder.text(R.id.tv_comment, "评论");
        holder.text(R.id.tv_read, "阅读量 " + 0);
        holder.image(R.id.iv_image, model.getUser().getAvatar());
        holder.text(R.id.tv_location, model.getJobUserAddress().getCity() + model.getJobUserAddress().getDistrict() + model.getJobUserAddress().getDetail());
        holder.click(R.id.card_view, view -> {
            if(itemClickListener!=null){
                itemClickListener.onItemClick( holder, view, model,  position);
            }
        });
        //Tag容器
        com.xuexiang.xui.widget.flowlayout.FlowLayout tagLayout = holder.findViewById(R.id.tag_layout);
        tagLayout.setTag(model.getJobId());
        tagLayout.removeAllViews();
        //判断实名认证
        if(model.getUser().getRealNameAuth()){
            addTag(tagLayout,"已实名","blue");
        }
        if(model.getUser().getEnterpriseAuth()){
            addTag(tagLayout,"企业认证","blue");
        }
        if(!StringUtils.isEmpty(model.getJobCategoryName())){
            addTag(tagLayout,model.getJobCategoryName(),"blue");
        }
        //结算方式
        if(!StringUtils.isEmpty(model.getSettlementModeName())){
            addTag(tagLayout,model.getSettlementModeName(),"blue");
        }
        //添加薪资
        if(!StringUtils.isEmpty(model.getSalaryName())){
            addTag(tagLayout,model.getSalaryName(),"blue");
        }

        for(String tag:model.getJobTagsList()){
            addTag(tagLayout,tag,"blue");
        }
        for(String tag:model.getWelfareTagsList()){
            addTag(tagLayout,tag,"gray");
        }
        for(String tag:model.getKeywordsList()){
            addTag(tagLayout,tag,"gray");
        }
    }
    private void addTag(com.xuexiang.xui.widget.flowlayout.FlowLayout linearLayout,String tagName,String themName){
        TextView textView = new TextView(linearLayout.getContext());
        ViewGroup.MarginLayoutParams textViewParams = new ViewGroup.MarginLayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, // 宽度
                ViewGroup.LayoutParams.WRAP_CONTENT  // 高度
        );
        textViewParams.setMargins(DensityUtils.dp2px(getContext(), 5), 0, 0, DensityUtils.dp2px(getContext(), 5));
        textView.setPadding(DensityUtils.dp2px(getContext(), 2),DensityUtils.dp2px(getContext(), 2),DensityUtils.dp2px(getContext(), 2),DensityUtils.dp2px(getContext(), 2));
        textView.setLayoutParams(textViewParams);
        textView.setText(tagName);
        textView.setTextSize(12.0f);

        if("blue".equals(themName)){
            textView.setBackgroundColor(linearLayout.getResources().getColor(R.color.tag_bg_blue));
            textView.setTextColor(linearLayout.getResources().getColor(R.color.colorPrimary));
        } else if ("gray".equals(themName)) {
            textView.setBackgroundColor(linearLayout.getResources().getColor(R.color.tag_bg_gray));
            textView.setTextColor(linearLayout.getResources().getColor(R.color.tag_text_gray));
        }else {
            textView.setBackgroundColor(linearLayout.getResources().getColor(R.color.tag_bg_blue));
            textView.setTextColor(linearLayout.getResources().getColor(R.color.colorPrimary));
        }
        linearLayout.addView(textView);
    }

    /**
     * 绑定占位控件
     *
     * @param holder
     * @param broccoli
     */
    @Override
    protected void onBindBroccoli(RecyclerViewHolder holder, Broccoli broccoli) {
        if (mIsAnim) {
            broccoli.addPlaceholder(PlaceholderHelper.getParameter(holder.findView(R.id.tv_user_name)))
                    .addPlaceholder(PlaceholderHelper.getParameter(holder.findView(R.id.tv_tag)))
                    .addPlaceholder(PlaceholderHelper.getParameter(holder.findView(R.id.tv_title)))
                    .addPlaceholder(PlaceholderHelper.getParameter(holder.findView(R.id.tv_summary)))
                    .addPlaceholder(PlaceholderHelper.getParameter(holder.findView(R.id.tv_praise)))
                    .addPlaceholder(PlaceholderHelper.getParameter(holder.findView(R.id.tv_comment)))
                    .addPlaceholder(PlaceholderHelper.getParameter(holder.findView(R.id.tv_read)))
                    .addPlaceholder(PlaceholderHelper.getParameter(holder.findView(R.id.iv_image)))
                    .addPlaceholder(PlaceholderHelper.getParameter(holder.findView(R.id.btn_link_boss)))
                    .addPlaceholder(PlaceholderHelper.getParameter(holder.findView(R.id.tag_layout)))
                    .addPlaceholder(PlaceholderHelper.getParameter(holder.findView(R.id.iv_location)))
                    .addPlaceholder(PlaceholderHelper.getParameter(holder.findView(R.id.tv_location)));

        } else {
            broccoli.addPlaceholders(
                    holder.findView(R.id.tv_user_name),
                    holder.findView(R.id.tv_tag),
                    holder.findView(R.id.tv_title),
                    holder.findView(R.id.tv_summary),
                    holder.findView(R.id.tv_praise),
                    holder.findView(R.id.tv_comment),
                    holder.findView(R.id.tv_read),
                    holder.findView(R.id.iv_image),
                    holder.findView(R.id.btn_link_boss),
                    holder.findView(R.id.tag_layout),
                    holder.findView(R.id.iv_location),
                    holder.findView(R.id.tv_location)
            );
        }
    }

    public void setItemClickListener(ItemClickListener<JobInfoVo> itemClickListener) {
        this.itemClickListener = itemClickListener;
    }
}
